package com.china08.yxyapi.api.home.kecheng.topic;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.db.lessons.YxyLessonsTopicCatalog;
import com.china08.yxyapi.entity.db.lessons.YxyLessonsTopicLesson;
import com.china08.yxyapi.entity.db.lessons.YxyLessonsTopicLessonChapter;
import com.china08.yxyapi.entity.db.lessons.YxyLessonsTopicLessonItem;
import com.china08.yxyapi.entity.db.lessons.YxyLessonsVideo;
import com.china08.yxyapi.entity.es.ElasticYxyResResource;
import com.china08.yxyapi.model.YxyApiPageReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.db.YxyLessonsTopicCatalogRepository;
import com.china08.yxyapi.repository.db.YxyLessonsTopicLessonChapterRepository;
import com.china08.yxyapi.repository.db.YxyLessonsTopicLessonItemRepository;
import com.china08.yxyapi.repository.db.YxyLessonsTopicLessonRepository;
import com.china08.yxyapi.repository.db.YxyLessonsVideoRepository;
import com.china08.yxyapi.service.yxyresource.impl.YxyResourceServiceImpl;

/**
 * 专题课程--详情页--上边和左边
 * 
 * @author LiuWeidong
 *
 */
@CrossOrigin
@RestController
public class KechengTopicParticularSrhController extends YxyApiBaseController {

    // 课程
    @Autowired
    private YxyLessonsTopicLessonRepository yxyLessonsTopicLessonRepository;

    // 分类
    @Autowired
    private YxyLessonsTopicCatalogRepository yxyLessonsTopicCatalogRepository;

    // 章节
    @Autowired
    private YxyLessonsTopicLessonChapterRepository yxyLessonsTopicLessonChapterRepository;

    // 课时
    @Autowired
    private YxyLessonsTopicLessonItemRepository yxyLessonsTopicLessonItemRepository;

    /** 视频 */
    @Autowired
    private YxyLessonsVideoRepository yxyLessonsVideoRepository;

    @Autowired
    private YxyResourceServiceImpl yxyResourceServiceImpl;

    @RequestMapping(value = "/api_yw/home/kecheng/topic/particular/srh", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public List<RespModel> find(HttpServletRequest request,
            HttpServletResponse response, @RequestBody ReqModel reqModel) {

        String lessonId = reqModel.lessonId;
        RespModel model = new RespModel();

        YxyLessonsTopicLesson yxyLessonsTopicLessons = yxyLessonsTopicLessonRepository
                .findById(lessonId);

        YxyLessonsTopicCatalog yxyLessonsTopicCatalogs1 = yxyLessonsTopicCatalogRepository
                .findByid(yxyLessonsTopicLessons.getCatalogId1());
        YxyLessonsTopicCatalog yxyLessonsTopicCatalogs2 = yxyLessonsTopicCatalogRepository
                .findByid(yxyLessonsTopicLessons.getCatalogId2());
        YxyLessonsTopicCatalog yxyLessonsTopicCatalogs3 = yxyLessonsTopicCatalogRepository
                .findByid(yxyLessonsTopicLessons.getCatalogId3());

        model.cover = yxyLessonsTopicLessons.getCover();
        model.name = yxyLessonsTopicLessons.getName();

        model.oneName = yxyLessonsTopicCatalogs1.getName();
        model.twoName = yxyLessonsTopicCatalogs2.getName();
        model.threeName = yxyLessonsTopicCatalogs3.getName();

        model.count = yxyLessonsTopicLessons.getCount();
        model.duration = yxyLessonsTopicLessons.getDuration();
        if (yxyLessonsTopicLessons.getEp() == null) {
            model.ep = 0f;
        } else {
            model.ep = yxyLessonsTopicLessons.getEp();
        }
        model.vv = yxyLessonsTopicLessons.getVv();
        model.summary = yxyLessonsTopicLessons.getSummary();
        model.author = yxyLessonsTopicLessons.getAuthor();
        model.avator = yxyLessonsTopicLessons.getAvator();
        model.vip = yxyLessonsTopicLessons.getVip();
        List<RespModel> returnLst = new ArrayList<>();

        // 章节、课时
        List<Chapter> chapterList = new ArrayList<Chapter>();
        List<YxyLessonsTopicLessonChapter> lessonChaptersLst = yxyLessonsTopicLessonChapterRepository
                .findByLessonIdOrderByOrderNum(lessonId);
        for (YxyLessonsTopicLessonChapter yxyLessonsTopicLessonChapter : lessonChaptersLst) {
            Chapter chapter = new Chapter();
            chapter.ChapterIntro = yxyLessonsTopicLessonChapter.getIntro();
            chapter.ChapterTitle = yxyLessonsTopicLessonChapter.getTitle();
            chapterList.add(chapter);

            List<LessonItem> lessonItemList = new ArrayList<LessonItem>();
            List<YxyLessonsTopicLessonItem> lessonItemLst = yxyLessonsTopicLessonItemRepository
                    .findByChapterIdAndStatusNotOrderByOrderNumAsc(
                            yxyLessonsTopicLessonChapter.getId(), -1);
            for (YxyLessonsTopicLessonItem yxyLessonsTopicLessonItem : lessonItemLst) {
                LessonItem lessonItem = new LessonItem();

                lessonItem.lessonItemId = yxyLessonsTopicLessonItem.getId();
                String weikeId = yxyLessonsTopicLessonItem.getWeikeId();
                // 先读weikeId
                if (null != weikeId) {
                    ElasticYxyResResource videos = yxyResourceServiceImpl
                            .findById(yxyLessonsTopicLessonItem.getWeikeId());
                    if (videos != null) {
                        lessonItem.duration = videos.getDuration();
                    }
                }

                // 如果weikeId为空，再读videoId
                else {
                    YxyLessonsVideo yxyLessonsVideos = yxyLessonsVideoRepository
                            .findById(yxyLessonsTopicLessonItem.getVideoId());
                    if (yxyLessonsVideos != null) {
                        lessonItem.duration = yxyLessonsVideos.getDuration();
                    }
                }

                lessonItem.lessonItemIntro = yxyLessonsTopicLessonItem
                        .getIntro();
                lessonItem.lessonItemTitle = yxyLessonsTopicLessonItem
                        .getTitle();
                lessonItem.preview = yxyLessonsTopicLessonItem.getPreview();
                lessonItem.lessonItemId = yxyLessonsTopicLessonItem.getId();
                lessonItem.OrderNum = yxyLessonsTopicLessonItem.getOrderNum();
                lessonItemList.add(lessonItem);
            }

            chapter.lessonItemList = lessonItemList;
            // chapterList.add(chapter);
        }
        model.chapterList = chapterList;
        returnLst.add(model);
        return returnLst;
    }

    /* 请求参数 */
    protected static class ReqModel extends YxyApiPageReqModel {

        // 课程ID
        public String lessonId;

    }

    /* 响应参数 */
    protected static class RespModel extends YxyApiRespModel {

        // 封面
        public String cover;
        // 课程名称
        public String name;

        // ###
        // 一级分类名称
        public String oneName;
        // 二级分类名称
        public String twoName;
        // 三级分类名称
        public String threeName;

        // ###
        // 总课时
        public Integer count;
        // 总时长
        public Integer duration;
        // 课程评分（所有评分之和处于评分人数即综合评分）
        public Float ep;
        // 学习人数
        public Integer vv;
        // 课程简介
        public String summary;
        // 贡献者
        public String author;
        // 贡献者头像
        public String avator;

        public Integer vip;

        // ###
        public List<Chapter> chapterList;

    }

    /** 章节 */
    public static class Chapter {

        // 章节名称
        public String ChapterTitle;
        // 章节简介
        public String ChapterIntro;

        public List<LessonItem> lessonItemList;
    }

    /** 课时 */
    public static class LessonItem {

        // 课时名称
        public String lessonItemTitle;
        // 课时简介
        public String lessonItemIntro;
        // 免费试看
        public Integer preview;
        // 课时Id
        public String lessonItemId;
        // 视频时长
        public Integer duration;
        // 序号
        public Integer OrderNum;
    }
}
